﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeoSwarm_Editor.Game;

namespace GeoSwarm_Editor
{
    public partial class EditWave : Form
    {

        private Wave wave;
        private List<Hexagonal.Hex> spawnHexes;
        private ListBox control_Waves;
        private Form parent;

        public EditWave(Wave wave, List<Hexagonal.Hex> spawnHexes, ListBox control_Waves, Form parent)
        {
            InitializeComponent();
            this.parent = parent;
            this.wave = wave;
            this.spawnHexes = spawnHexes;
            this.control_Waves = control_Waves;
            Hexagonal.Hex[] spawnsTemp = new Hexagonal.Hex[spawnHexes.Count];
            spawnHexes.CopyTo(spawnsTemp, 0);
            control_WaveSpawnHex.Items.AddRange(spawnsTemp);
            LoadWaveData();
            ValidateWaveData();

            control_CreepType.SelectedIndex = 0;

            this.Show();
        }

        private void LoadWaveData()
        {
            control_WaveSpawnHex.SelectedIndex = control_WaveSpawnHex.Items.IndexOf(wave.SpawnHex);
            control_Concurrent.Checked = wave.Concurrent;
            control_WaveCreeps.Items.AddRange(wave.Creeps);
        }

        private void button_SaveWave_Click(object sender, EventArgs e)
        {
            wave.ClearCreeps();
            wave.AddCreeps(control_WaveCreeps.Items);
            wave.SpawnHex = (Hexagonal.Hex)control_WaveSpawnHex.SelectedItem;
            RefreshWavesBox();
            CloseDialog();
        }

        private void RefreshWavesBox()
        {
            int thisItem = control_Waves.Items.IndexOf(wave);
            control_Waves.Items.RemoveAt(thisItem);
            control_Waves.Items.Insert(thisItem, wave);
            control_Waves.SelectedIndex = thisItem;
        }

        private void ValidateWaveData()
        {
            if (control_WaveSpawnHex.SelectedItem == null)
                button_SaveWave.Enabled = false;
            else if (control_WaveCreeps.Items.Count > 0)
                button_SaveWave.Enabled = true;
            else
                button_SaveWave.Enabled = false;

            if (control_WaveCreeps.SelectedIndex >= 0)
                button_RemoveCreep.Enabled = true;
            else
                button_RemoveCreep.Enabled = false;
        }

        private void control_WaveSpawnHex_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateWaveData();
        }

        private void control_CreepType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateWaveData();
        }

        private void control_CreepsInWave_ValueChanged(object sender, EventArgs e)
        {
            ValidateWaveData();
        }

        private void control_WaveSpawnHex_DropDownClosed(object sender, EventArgs e)
        {
            ValidateWaveData();
        }

        private void control_WaveCreeps_SelectedIndexChanged(object sender, EventArgs e)
        {
            ValidateWaveData();
            control_CountBox.Text = string.Empty;
            if(control_WaveCreeps.SelectedIndex >= 0 && control_WaveCreeps.SelectedItem != null)
            {
                Creep sel = (Creep)control_WaveCreeps.SelectedItem;
                control_CountBox.Text = sel.Count.ToString();
            }
        }

        private void button_RemoveCreep_Click(object sender, EventArgs e)
        {
            int oldInd = control_WaveCreeps.SelectedIndex;
            wave.RemoveCreep(control_WaveCreeps.SelectedIndex);
            control_WaveCreeps.Items.RemoveAt(oldInd);
            if (oldInd > 0 && control_WaveCreeps.Items.Count > 0)
                control_WaveCreeps.SelectedIndex = oldInd - 1;
            else if(control_WaveCreeps.Items.Count > 0)
                control_WaveCreeps.SelectedIndex = 0;
        }

        private void button_AddCreep_Click(object sender, EventArgs e)
        {
            Creep addedCreep = wave.AddCreep(control_CreepType.SelectedIndex, (int)control_CreepsInWave.Value);
            control_WaveCreeps.Items.Add(addedCreep);
            ValidateWaveData();
        }

        private void button_RemoveWave_Click(object sender, EventArgs e)
        {
            int thisItem = control_Waves.Items.IndexOf(wave);
            control_Waves.Items.RemoveAt(thisItem);
            CloseDialog();
        }

        private void CloseDialog()
        {
            parent.Enabled = true;
            this.Close();
            this.Dispose();
        }

        private void control_Concurrent_CheckedChanged(object sender, EventArgs e)
        {
            if (control_Concurrent.Checked)
                wave.Concurrent = true;
            else
                wave.Concurrent = false;
        }

    }
}
